import names
import random
import threading
from sqlalchemy import create_engine
from datamaster import Db

SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:Supertom2019!@iot-super.com/sale_manager?charset=utf8'

address_list = {'Toronto', 'Hamilton', 'Waterloo'}

car_model = ['ILX', 'TLX', 'RLX', 'RDX', 'MDX', 'NSX']
produce_year = [2019, 2020]

customer_list = []


class CustomerGeneratorThread(threading.Thread):
    def __init__(self, counter):
        threading.Thread.__init__(self)
        self.counter = counter

    @staticmethod
    def random_phone_number():
        cell_phone = ''
        for j in range(10):
            cell_phone += str(random.randint(0, 9))
        return cell_phone

    @staticmethod
    def random_birthday():
        # for testing only, date from 1st to 28th
        year = str(random.randint(1920, 2000))
        month = str(random.randint(1, 12))
        date = str(random.randint(1, 28))
        return year + "-" + month + "-" + date

    def run(self):
        address = random.choice(address_list)

        customer_list.append(
            {'first_name': names.get_first_name(), 'last_name': names.get_last_name(),
             'birthday': self.random_birthday(), 'phone_number': self.random_phone_number(),
             'address': address})


class Initializer:
    engine = create_engine(SQLALCHEMY_DATABASE_URI, encoding='utf8', echo=True, pool_pre_ping=True)
    db = Db(engine)

    @staticmethod
    def init_customers(s):
        s.execute('DELETE FROM customers')
        threads = []
        for i in range(1):
            for j in range(1):
                temp_thread = CustomerGeneratorThread(j)
                temp_thread.start()
                threads.append(temp_thread)
            for t in threads:
                t.join()
            s.add_many('customers', customer_list)
            customer_list.clear()
            threads.clear()

    @staticmethod
    def init_dealers(s):
        s.execute('DELETE FROM dealers')
        first_dealer = {
            'dealer_name': 'Ai-Acura',
            'phone_number': '9057865432',
            'address': 'Toronto'
        }
        s.add('dealers', first_dealer)

    @staticmethod
    def init_models(s):
        s.execute('DELETE FROM models')
        model_list = []
        for m_type in car_model:
            for m_year in produce_year:
                temp_model = {
                    'car_model': m_type,
                    'production_year': m_year,
                    'number_in_stock': 100
                }
                model_list.append(temp_model)
        s.add_many('models', model_list)
        # special setting:
        s.execute(f"""UPDATE models SET number_in_stock=3 WHERE car_model='NSX'""")

    @staticmethod
    def init_orders(s):
        s.execute('DELETE FROM orders')

    def init_all(self):
        with self.db.session() as s:
            self.init_customers(s)
            self.init_dealers(s)
            self.init_models(s)
            self.init_orders(s)


initializer = Initializer()
initializer.init_all()
